%%%----------------------------------------------------------------
%%% @author  H. Kerem Cevahir <hkerem@gmail.com>
%%% @doc
%%% @end
%%% @copyright 2009 H. Kerem Cevahir
%%%----------------------------------------------------------------
-module(hdb_serv).

-behaviour(gen_server).

-export([start_link/0]).
-export([scan_hdb/1]).
-export([init/1, handle_call/3]).

-include("file.hrl").
-include("tables.hrl").

start_link() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

scan_hdb(#file{} = File) ->
	gen_server:call(?MODULE, {scan_hdb, File}, 100000).

init(_Args) ->	
	{ok, 0}.

query_result_to_call_result([_Row]) ->
	infected;
query_result_to_call_result([]) ->
	file_clean.

handle_call({scan_hdb, #file{} = File}, _From, State) ->
	F = fun() -> mnesia:read({hdb,md5:bin(File#file.data)}) end,
	{atomic, QRes} = mnesia:transaction(F),
	{reply, query_result_to_call_result(QRes), State}.

